package com.juyu.miliao.api.service;

import com.juyu.miliao.api.dao.mysql.FakeUserDao;
import com.juyu.miliao.api.domain.FakeUser;
import com.juyu.miliao.api.domain.dto.FakeUserDTO;
import com.juyu.miliao.api.util.DateUtil;
import org.dozer.DozerBeanMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jh
 * 2018/10/19
 */
@Service
public class FakeUserService {

    /**
     * 魅力榜单  女
     */
    private final Map<String, List<FakeUserDTO>> charmMap = Collections.synchronizedMap(new HashMap<>(40));
    /**
     * 贡献榜单  男
     */
    private final Map<String, List<FakeUserDTO>> contributionMap = Collections.synchronizedMap(new HashMap<>(40));
    @Resource
    private FakeUserDao fakeUserDao;
    @Resource
    private DozerBeanMapper dozerBeanMapper;

    private static int getRandomDiamond() {
        int min = 30000;
        int max = 100000;
        return min + (int) (Math.random() * (max - min + 1));
    }

    private static int getRandomGold() {
        int min = 40000;
        int max = 80000;
        return min + (int) (Math.random() * (max - min + 1));
    }

    private static void addSort(List<FakeUserDTO> dtos) {
        Collections.sort(dtos);
        int i = 1;
        for (FakeUserDTO dto : dtos) {
            dto.setSort(i++);
        }
    }

    /**
     * 获取榜单
     *
     * @param type 1-魅力榜 2-贡献榜
     * @return list
     */
    public List<FakeUserDTO> list(int type) {
        String dayString = DateUtil.getTodayString();
        List<FakeUserDTO> list;
        if (type == 1) {
            list = charmMap.get(dayString);
            //双重锁
            if (list == null) {
                synchronized (this) {
                    if (list == null) {
                        list = update(1, dayString);
                    }
                }

            }
            return list;
        }

        list = contributionMap.get(dayString);
        if (list == null) {
            synchronized (this) {
                if (list == null) {
                    list = update(2, dayString);
                }
            }
        }
        return list;
    }

    /**
     * 根据日期更新
     *
     * @param type      type 1-魅力榜 2-贡献榜
     * @param dayString 日期
     * @return
     */
    private List<FakeUserDTO> update(int type, String dayString) {
        //钻石
        if (type == 1) {
            charmMap.clear();
            List<FakeUser> list = fakeUserDao.listRandomFakeUser(30, false);
            List<FakeUserDTO> dtos = new ArrayList<>();
            for (FakeUser fakeUser : list) {
                FakeUserDTO dto = dozerBeanMapper.map(fakeUser, FakeUserDTO.class);
                int random = getRandomDiamond();
                dto.setDiamond(random);
                dto.setCompareNum(random);
                dtos.add(dto);
            }
            addSort(dtos);
            charmMap.put(dayString, dtos);
            return dtos;
        }

        //金币
        contributionMap.clear();
        List<FakeUser> list = fakeUserDao.listRandomFakeUser(30, true);
        List<FakeUserDTO> dtos = new ArrayList<>();
        for (FakeUser fakeUser : list) {
            FakeUserDTO dto = dozerBeanMapper.map(fakeUser, FakeUserDTO.class);
            int random = getRandomGold();
            dto.setGoldCorn(random);
            dto.setCompareNum(random);
            dtos.add(dto);
        }
        addSort(dtos);
        contributionMap.put(dayString, dtos);
        return dtos;
    }

}
